package com.wyl.wyl.testapplication.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.utils.base.Utils;
import com.wyl.wyl.testapplication.Api;
import com.wyl.wyl.testapplication.R;
import com.wyl.wyl.testapplication.adapter.FunctionListAdapter;
import com.wyl.wyl.testapplication.base.BaseActivity;
import com.wyl.wyl.testapplication.bean.CheckVersionBean;
import com.wyl.wyl.testapplication.bean.FunctionItem;
import com.wyl.wyl.testapplication.bean.PermissonResponseBean;
import com.wyl.wyl.testapplication.bean.RequestBean;
import com.wyl.wyl.testapplication.utils.ActivityUtils;
import com.wyl.wyl.testapplication.utils.CollectionUtils;
import com.wyl.wyl.testapplication.utils.UpgradeVersionDialogUtils;
import com.wyl.wyl.testapplication.utils.request.RequestCallback;
import com.wyl.wyl.testapplication.utils.request.RequestUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class MainActivity extends BaseActivity {

    private GridView functionList;
    private Activity mActivity;
    private List<FunctionItem> functionItemList;
    private TextView title;
    private LinearLayout back;
    private Call<CheckVersionBean> call;
    private ProgressDialog dialog;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = this;
        dialog = new ProgressDialog(MainActivity.this);
        initView();
        initData();
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void initData() {
//        setData(true);
        checkVersion();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.headerTitle);
        setTitle("川维大屏");
        functionList = (GridView) findViewById(R.id.functionList);

    }

    private void setData(boolean flag) {
        functionItemList = ActivityUtils.getFunctionItemList(mActivity, flag);
        FunctionListAdapter functionListAdapter = new FunctionListAdapter(mActivity, functionItemList);
        functionList.setAdapter(functionListAdapter);
    }

    private void checkVersion() {

        RequestUtils.getRetrofit(MainActivity.this).create(Api.class).checkVersion(String.valueOf(Utils.getVersionCode(MainActivity.this)), "1")
                .enqueue(new RequestCallback<CheckVersionBean>(MainActivity.this) {
                    @Override
                    public void success(Response<CheckVersionBean> response) {
                        Gson gson = new Gson();
                        Log.i("aaa", gson.toJson(response.body(), CheckVersionBean.class));
                        CheckVersionBean.BodyBean.VersionBean version = response.body().getBody().getVersion();
                        if (version.getVersionCode() == null) {
                            getFunction();
                        } else {
                            Dialog dialog = UpgradeVersionDialogUtils.getUpgradeVersionDialog(MainActivity.this, version.getSize(), version.getVersionName(), version.getVersionDescription(), version.getUrl(), version.getMd5(), null);
                            dialog.setCancelable(false);
                            dialog.show();
                        }
                    }
                });
    }

    private void getFunction() {
        RequestBean bean = new RequestBean();
        bean.setEquipment_code(CollectionUtils.getDuid(MainActivity.this));
        RequestUtils.getRetrofit(MainActivity.this).create(Api.class).getPermission(bean)
                .enqueue(new RequestCallback<PermissonResponseBean>(MainActivity.this) {
                    @Override
                    public void success(Response<PermissonResponseBean> response) {
                        if (response.body().getBody().getFlag().equals("1")) {
                            setData(true);
                        } else {
                            setData(false);
                        }
                    }

                    @Override
                    public void failure(Response<PermissonResponseBean> response) {
                        super.failure(response);
                        setData(true);
                    }
                });
    }

}
